package com.sd.reader;

import android.content.Context;
import android.graphics.Paint;
import android.text.SpannableString;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.alex.books.BookPageFactory;
import com.sd.activity.R;
import com.sd.util.ReadUtil;

/**
 * 
 * 
 * @author Alex.Lu
 *
 */
public class ReaderFlipper extends ViewFlipper implements OnGestureListener, OnTouchListener, OnClickListener {

	private final String TAG = "HorizontalFlipper";
	private int mFliperDirection;
	private Context mContext;
	private GestureDetector mDetector;
	
	/**
	 * 当前
	 */
	private TextView mCurText;
	private TextView mCurTitle;
	private TextView mCurBottom;
	/**
	 * 
	 */
	private TextView mNextText;
	private TextView mNextTitle;
	private TextView mNextBottom;
	private OnClickListener mOnClickListener;
	
	private int count;
	
	public ReaderFlipper(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initView(context);
	}
	
	public ReaderFlipper(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		 initView(context);
	}
	
	
	private void initView(Context context){
		setId(R.id.reader);
		mContext = context;
		mDetector = new GestureDetector(this);
		this.setOnTouchListener(this);
		this.setOnClickListener(this);
		
		RelativeLayout.LayoutParams contentParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		RelativeLayout.LayoutParams bottomParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		bottomParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		bottomParams.bottomMargin = 5;
		
		RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		titleParams.leftMargin = 20;
		titleParams.topMargin = 5;
		
		RelativeLayout curRel = new RelativeLayout(context);
		mCurText = new TextView(mContext);
		mCurText.setTextColor(0xff000000);
		curRel.addView(mCurText,contentParams);
		
		mCurTitle = new TextView(context);
		mCurTitle.setSingleLine(true);
		mCurTitle.setEllipsize(TruncateAt.END);
		curRel.addView(mCurTitle,titleParams);
		
		mCurBottom = new TextView(context);
		mCurBottom.setSingleLine(true);
		mCurBottom.setEllipsize(TruncateAt.END);
		mCurBottom.setGravity(Gravity.CENTER_HORIZONTAL);
		curRel.addView(mCurBottom,bottomParams);
		
		RelativeLayout nextRel = new RelativeLayout(context);
		mNextText = new TextView(mContext);
		mNextText.setTextColor(0xff000000);
		nextRel.addView(mNextText,contentParams);
		
		mNextTitle = new TextView(context);
		mNextTitle.setSingleLine(true);
		mNextTitle.setEllipsize(TruncateAt.END);
		nextRel.addView(mNextTitle,titleParams);
		
		mNextBottom = new TextView(context);
		mNextBottom.setSingleLine(true);
		mNextBottom.setEllipsize(TruncateAt.END);
		mNextBottom.setGravity(Gravity.CENTER_HORIZONTAL);
		nextRel.addView(mNextBottom,bottomParams);
		
		addView(curRel);
		addView(nextRel);
	}
	
	int mWidth, mHeight;
	public void init(final int screenW, final int screenH, float density){
		mWidth = screenW;
		mHeight = screenH;
	}

	
	/**
	 * 设置字体
	 */
	public void setTypeface(){
		String family = ReaderSet.getInstance().getFontFamily();
		int fontStyle = ReaderSet.getInstance().getFontStyle();
		if(fontStyle == Paint.UNDERLINE_TEXT_FLAG){
			mCurText.getPaint().setUnderlineText(true);
			mNextText.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		}
	
		BookPageFactory.getInstance().setFontFamilly(family,fontStyle);
	}
	
	public void setReaderTextSize(float size,float density){
		mCurText.setPadding(20, (int)(30*density), 20, 0);
		mNextText.setPadding(20, (int)(30*density), 20, 0);
		mNextText.setTextSize(size);
		mCurText.setTextSize(size);
		BookPageFactory.getInstance().setFontSize(size * density);
	}
	
	public void setLineSpace(float size){
		BookPageFactory.getInstance().setLineSpace(size);
		mNextText.setLineSpacing(size /2, 1f);
		mCurText.setLineSpacing(size /2, 1f);
	}
	
	/**
	 * 设置主题
	 * @param resId
	 * @param color
	 */
	public void setReaderBg(int resId,int color){
		mCurText.setBackgroundResource(resId);
		mNextText.setBackgroundResource(resId);
		mCurText.setTextColor(color);
		mNextText.setTextColor(color);
		mCurTitle.setTextColor(color);
		mNextTitle.setTextColor(color);
		mCurBottom.setTextColor(color);
		mNextBottom.setTextColor(color);
	}
	/**
	 * 设置切换的方向
	 * @param dir 
	 */
	public void setReaderDir(int dir){
		mFliperDirection = dir;
	}
	
	/**
	 * 上下切换
	 * @param e1
	 * @param e2
	 * @param velocityX
	 * @param velocityY
	 * @return
	 */
	private boolean updownFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY){
		boolean flag = false;
		if (e1.getY() - e2.getY() > 50) {
			this.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_up_in));
			this.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_up_out));
			flag = nextPage();
			if(flag){
				this.showNext();
			}
			return flag;
		} else if (e1.getY() - e2.getY() < -50) {
			this.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_down_in));
			this.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_down_out));
			flag = prePage();
			if(flag){
				this.showNext();
			}
			return flag;
		}
		return false;
	}
	
	
	private boolean rightleftFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY){
		boolean flag = false;
		if (e1.getX() - e2.getX() > 50) {
			this.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_left_in));
			this.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_left_out));
			flag = nextPage();
			if(flag){
				this.showNext();
			}
			return flag;
		} else if (e1.getX() - e2.getX() < -50) {
			this.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_right_in));
			this.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_right_out));
			flag = prePage();
			if(flag){
				this.showNext();
			}
			return flag;
		}
		
		return false;
	}
	
	public void reFreshPage(){
		SpannableString text = BookPageFactory.getInstance().refreshPage();
		mCurBottom.setText(ReadUtil.getPercent(BookPageFactory.getInstance().getBook()));
		mCurTitle.setText(BookPageFactory.getInstance().getBook().mReaderingInfo.mCurChapter.mChapterName);
		if(count%2!=0){
			mCurText.setText(text);
		}else{
			mNextText.setText(text);
		}
	}
	
	public boolean nextPage(){
		
		//String text = BookPageFactory1.getInstance().nextPage();
		
		if(BookPageFactory.getInstance().isLastPage()){
			Toast.makeText(mContext, "已经是最后一页了", Toast.LENGTH_SHORT).show();
			return false;
		}else{
			SpannableString text = BookPageFactory.getInstance().nextPage();
			
			if(count%2==0){
				mCurBottom.setText(ReadUtil.getPercent(BookPageFactory.getInstance().getBook()));
				mCurTitle.setText(BookPageFactory.getInstance().getBook().mReaderingInfo.mCurChapter.mChapterName);
				mCurText.setText(text);
			}else{
				mNextBottom.setText(ReadUtil.getPercent(BookPageFactory.getInstance().getBook()));
				mNextTitle.setText(BookPageFactory.getInstance().getBook().mReaderingInfo.mCurChapter.mChapterName);
				mNextText.setText(text);
			}
			count++;
			return true;
		}
	}
	
	
	private boolean prePage(){
		//String text = BookPageFactory1.getInstance().previousPage();
		
		if(BookPageFactory.getInstance().isFirstPage()){
			Toast.makeText(mContext, "已经是第一页了", Toast.LENGTH_SHORT).show();
			return false;
		}else{
			SpannableString text = BookPageFactory.getInstance().previousPage();
			
			if(count%2==0){
				mCurBottom.setText(ReadUtil.getPercent(BookPageFactory.getInstance().getBook()));
				mCurTitle.setText(BookPageFactory.getInstance().getBook().mReaderingInfo.mCurChapter.mChapterName);
				mCurText.setText(text);
			}else{
				mNextBottom.setText(ReadUtil.getPercent(BookPageFactory.getInstance().getBook()));
				mNextTitle.setText(BookPageFactory.getInstance().getBook().mReaderingInfo.mCurChapter.mChapterName);
				mNextText.setText(text);
			}
			count++;
			return true;
		}
		
	}
	
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if(mFliperDirection == ReaderConfig.UPDOWN_DIR){
			updownFling(e1,e2,velocityX,velocityY);
		}else if(mFliperDirection == ReaderConfig.RIGHTLEFT_DIR){
			rightleftFling(e1,e2,velocityX,velocityY);
		}
		return false;
	}

	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		
		if(e.getAction() == MotionEvent.ACTION_UP 
		&& (mWidth/4) < e.getX()
		&& e.getX() < (mWidth*3/4)
		&& (mHeight/3) < e.getY()
		&& e.getY() < (mHeight*2/3)){
			
			mOnClickListener.onClick(this);
		}
		return false;
	}

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		mDetector.onTouchEvent(event);
		return super.onTouchEvent(event);
	}

	
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}
	
	public void setOnSingleTapUpListener(OnClickListener onClickListener){
		mOnClickListener = onClickListener;
	}
	
}
